package com.example.controller;

import com.example.pojo.*;
import com.example.service.OrderService;
import com.example.service.impl.GoodServiceImpl;
import com.example.service.impl.OrderDetailsServiceImpl;
import com.example.service.impl.OrderServiceImpl;
import com.example.tools.JwtUtils;
import com.example.tools.Result;
import com.example.service.impl.GoodTypeImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin")
public class OrderController {

    @Autowired
    private GoodServiceImpl goodService;
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private OrderDetailsServiceImpl detailsService;

    /**
     * 获取订单信息
     * @return
     */
    @PostMapping("/getOrder")
    public Result getOrder(
        @RequestBody OrdSearch order,
        @RequestParam(defaultValue = "1") Integer page,
        @RequestParam(defaultValue = "10") Integer pageSize) {

            PageBean pageBean = orderService.getOrder(order,page, pageSize);
            return Result.success(pageBean);
    }

    /**
     * 修改供应商信息
     */
    @PostMapping("/changeOrder")
    public Result changeOrder(@RequestBody Order order){
        orderService.changeOrder(order);

        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteOrder")
    public Result deleteOrder(String id){
        orderService.deleteOrder(id);
        return Result.success();
    }

    /**
     * 添加供应商信息
     * @param order
     * @return
     */
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Order order){
        orderService.addOrder(order);
        return Result.success();
    }

    @PostMapping("/good/buy")
    @Transactional
    public Result buy(@RequestBody List<Good> good, @RequestHeader("Token") String token, Double total){
        //获取客户id
        String s = JwtUtils.parseJWT(token).get("cusCode", String.class);
        Integer cusCode = Integer.parseInt(s);
        //生成订单
        Order order=new Order(null,cusCode, LocalDateTime.now(),total);
        Integer ordCode = orderService.addOrder(order);
        log.info("订单编号,{}",ordCode);
        //构造明细list
        List<OrderDetails>orderDetails=new ArrayList<>();
        for (int i = 0; i < good.size(); i++) {
            String goodCode = good.get(i).getGoodCode();
            Double selPrice = good.get(i).getSelPrice();
            Integer sealNum = good.get(i).getSealNum();
            goodService.numChange(goodCode,sealNum);
            OrderDetails details = new OrderDetails(null, ordCode, goodCode, selPrice, sealNum, selPrice * sealNum,null);
            orderDetails.add(details);
        }
        //生成订单明细
        log.info("订单明细,{}",orderDetails);

        detailsService.addOrderDetails(orderDetails);



        return Result.success();
    }


}
